

package ex01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ShowTable {
    
    public static void main(String[] args) {
        
        Connection con = null;
        Statement st = null;
        
        try {
            //1. Carregar driver da BD a user. Driver especifico
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            //2. Obter uma Connection
            String connStr = "jdbc:odbc:DRIVER={Microsoft access Driver (*.mdb)}; DBQ=Northwind97.mdb";            
            con = DriverManager.getConnection(connStr);
            
            //3. Criar Statement            
            st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM EMPLOYEES");
            
            //getMetaData -> devolve o nome dos campos, tipo de dados
            ResultSetMetaData rsm = rs.getMetaData();
            
            //saber quantos campos existem na tabela
            int numCampos = rsm.getColumnCount();
                        
            for (int i = 1; i<= numCampos; i++)
                System.out.print(rsm.getColumnName(i) + "\t");            
                System.out.println("\n ====================================");
                while(rs.next())
                    for (int i = 1; i<= numCampos; i++)
                        System.out.print(rs.getString(i) + " ");
                              
        } catch (SQLException ex) {
            System.out.println("Erro:" + ex.getMessage());
        } catch (ClassNotFoundException ex) {            
            System.err.println("Erro: Driver não foi encontrado");            
        } finally {
            try {
                if (st != null) st.close();
                if (con != null) con.close();
            } catch (SQLException ex) {
                System.out.println(ex);
            } 
        }  
                                    
    }

}
